/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   ChangeInvestServiceImpl.java for com.pengtu.gsj.service.impl.check.jhgl    </p>
 * 
 * <p>最后修改 : $: 2017年7月21日-上午11:14:34 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jhgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jhgl.ChangeInvestDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jhgl.NewChangeInvest;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jhgl.ChangeInvestService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * ChangeInvestServiceImpl
 * 
 * 2017年7月21日 上午11:14:34
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class ChangeInvestServiceImpl implements ChangeInvestService {
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public ChangeInvestDao changeInvestDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	
	public List<NewChangeInvest> getChangeInvestList(QueryFilter queryFilter){
		return changeInvestDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getChangeInvest(String startTime, String endTime, CheckTable checkTable, String tag, Boolean flag) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<NewChangeInvest> changeInvestList = new ArrayList<NewChangeInvest>();
		List<Attachment> list = new ArrayList<Attachment>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//通报一个项目进度缓慢扣分
		double yearFull =  Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart =  Double.parseDouble((String)jsonObject.get("yearPart"));
		
		//返回时间
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(startTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("year", startTime, MatchType.GE);
		queryFilter.addFilter("year", endTime, MatchType.LE);
		changeInvestList = this.getChangeInvestList(queryFilter);
		
		//检查记录
		double total = 0.0;				//年度累计得分
		double percentage = 0.0;		//完善度
		double temp = 0.0;				//总分
		double tableFull = MathsUtils.formatDouble(yearPart*years.length, 2);        //满分
		for (NewChangeInvest newChangeInvest : changeInvestList) {
			double slowMark = 0.0;
			if (newChangeInvest.getSlowNum() > 0) {
				slowMark = MathsUtils.mulDouble(yearFull,  newChangeInvest.getSlowNum());
				slowMark = MathsUtils.formatDouble(slowMark,2);
			} 
			if (newChangeInvest.getInvestPate() != 0.0) {
				total = MathsUtils.divDouble(newChangeInvest.getInvestPate(), 100.00, 2);
				total = MathsUtils.subDouble(yearPart*total, slowMark);
				total = MathsUtils.formatDouble(total, 2);
			} 
			
			if (total < 0.0) {
				total = 0.0;
			}
			temp = MathsUtils.sumDouble(temp, total);
			newChangeInvest.setYearMark(total);
		}
		map.put("sum", temp);
		//判断完善度
		percentage = MathsUtils.divDouble(temp, tableFull, 2);
		percentage = MathsUtils.mulDouble(percentage, 100);
		map.put("percent", percentage);
								
		//得到所有文件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		map.put("checkTable", checkTable);
		map.put("changeInves", changeInvestList);
		map.put("years", years);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("score", checkTable.getTestFull());
		return map;
	}

}
